package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.KnowledgeFood;
import com.itheima.pojo.KnowledgeSport;
import com.itheima.service.KnowledgeSportService;
import com.itheima.utils.POIUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;

/**
 *知识库运动库
 */
@RestController
@RequestMapping("/knowledgesport")
public class KnowledgeSportController {
    @Reference
    private KnowledgeSportService knowledgeSportService;

    /**
     * 分页查询运动数据
     * @param queryPageBean
     * @return
     */
    @RequestMapping("/findPage.do")
    @PreAuthorize("hasAuthority('SPORT_QUERY')")
    public PageResult  findPage (@RequestBody QueryPageBean queryPageBean){
        return knowledgeSportService.findPage(queryPageBean);
    }

    /**
     * 添加运动数据
     * @param knowledgeSport
     * @return
     */
    @RequestMapping("/add.do")
    @PreAuthorize("hasAuthority('SPORT_ADD')")
    public Result add (@RequestBody KnowledgeSport knowledgeSport){
        try {
            knowledgeSportService.add(knowledgeSport);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,"添加运动失败");
        }
        return new Result(true,"添加运动成功!");
    }

    /**
     * 编辑修改运动数据
     * @param knowledgeSport
     * @return
     */
    @RequestMapping("/edit.do")
    @PreAuthorize("hasAuthority('SPORT_EDIT')")
    public Result edit (@RequestBody KnowledgeSport knowledgeSport){
        try {
            knowledgeSportService.edit(knowledgeSport);
            return new Result(true,"修改运动数据成功!");
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,"修改运动数据失败");
        }
    }

    /**
     * 删除运动数据
     * @param id
     * @return
     */
    @RequestMapping("/delete.do")
    @PreAuthorize("hasAuthority('FOOD_DELETE')")
    public Result delete(Integer id){
        try {
            knowledgeSportService.delete(id);
            return new Result(true,"删除运动数据成功!");
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,"删除运动数据失败");
        }
    }

    @RequestMapping("/upload.do")
    public Result upload(@RequestParam("excelFile") MultipartFile multipartFile){
        try {
            List<String[]> list = POIUtils.readExcel(multipartFile);
            ArrayList<KnowledgeSport> knowledgeSports = new ArrayList<>();
            for (String[] strings : list) {
                //添加数据
                knowledgeSports.add(new KnowledgeSport(strings[0],strings[1],strings[2],strings[3]));
            }
            //调用业务层添加数据或者更新
            for (KnowledgeSport knowledgeSport : knowledgeSports) {
                knowledgeSportService.add(knowledgeSport);
            }
            return new Result(true, MessageConstant.UPLOAD_SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,"上传失败");
        }
    }

    @RequestMapping("/findAll.do")
    public Result findAll(){
        try {
            List<KnowledgeSport> knowledgeSports = knowledgeSportService.findAll();
            return new Result(true,"获取膳食所有数据成功",knowledgeSports);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,"获取膳食所有数据失败");
        }

    }
}
